package cn.sdormitory.controller.report;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.report.service.RepairStatisticsService;
import cn.sdormitory.report.vo.RepairStatisticsVO;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2020/12/5 12:31
 * @version：V1.0
 */
@RestController
@Api(tags = "Report-RepairStatistics=> 报修统计")
@RequestMapping("/report/repairstatistics")
public class RepairStatisticsController {
    @Autowired
    private RepairStatisticsService repairStatisticsService;

    @ApiOperation("list => 查询报修统计列表")
    @PreAuthorize("@ss.hasPermi('report:repairstatistics:list')")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<RepairStatisticsVO>> list(@RequestParam Map<String, Object> params) {
        String yearMonth=(String)params.get("inYearMonth");
        String inYearMonth="";
        if("".equals(yearMonth) || yearMonth==null){
            Date date = new Date();
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
            String date1=fmt.format(date);
            inYearMonth=date1;
        }else{
            inYearMonth=yearMonth;
        }
        List<RepairStatisticsVO> repairStatisticsVOList = repairStatisticsService.getRepairStaList(inYearMonth,Integer.parseInt((String)params.get("pageNum")),Integer.parseInt((String)params.get("pageSize")));
        if(repairStatisticsVOList!=null && repairStatisticsVOList.size()>0){
            for (RepairStatisticsVO repairStatisticsVO:repairStatisticsVOList){
                repairStatisticsVO.setInYearMonth(inYearMonth);
            }
        }
        CommonPage<RepairStatisticsVO> commonPage = new CommonPage<RepairStatisticsVO>();
        commonPage.setList(repairStatisticsVOList);
        Long count=repairStatisticsService.getRepairStaListCount(inYearMonth);
        commonPage.setTotal(count);
        return CommonResult.success(commonPage);

    }

    @ApiOperation("repairlist =>导出宿舍报修统计信息")
    @PreAuthorize("@ss.hasPermi('report:repairstatistics:repairlist')")
    @GetMapping(value = "/exportrepairlist")
    public CommonResult export(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        String yearMonth=(String)params.get("inYearMonth");
        String inYearMonth="";
        if("".equals(yearMonth) || yearMonth==null){
            Date date = new Date();
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
            String date1=fmt.format(date);
            inYearMonth=date1;
        }else{
            inYearMonth=yearMonth;
        }
        List<RepairStatisticsVO> repairStatisticsVOList = repairStatisticsService.getRepairStaListExport(inYearMonth);
        try {
            EasyExcel.write(response.getOutputStream(), RepairStatisticsVO.class).sheet("宿舍报修统计管理").doWrite(repairStatisticsVOList);
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }
}
